#include "JCommandLine.h"
#include "JFile.h"
#include "JSerialize.h"
#include "JTree.h"
#include "JClientUtility.h"
#include "JTime.h"
#include "JConfig.h"
#include "JSerialize.h"
#include "JWorkingCopies.h"
#include "JString.h"
#include <iostream>
#include <stdio.h>
#include "JClientLibrary.h"

JCommandLine::JCommandLine(void)
{

}


JCommandLine::~JCommandLine(void)
{
}

void JCommandLine::Run(int argc,char* argv[])
{
	std::string	currentWorkingPath = boost::filesystem::current_path().string();
	std::string command;
	if(argc >= 2)
	{
		command = JString::ToSmaller(argv[1]);
	}
	switch(argc)
	{
	case 1:
		printf("Type \"jsvn help\" for help.");
		break;
	case 2:
		break;
	case 3:
		//jsvn add foo;jsvn delete foo;create D:\test
		if (command == "add")
		{
			if(argc != 3)
			{
				printf("Type \"jsvn help add\" for help.");
			}
			else
			{
				JClientLibrary lib;
				PrintOperateResult(lib.BasicOperAddFile(argv[2]));
				
			}
		}
		else if(command == "delete")
		{
			if(argc != 3)
			{
				printf("Type \"jsvn help delete\" for help.");
			}
			else
			{
				JClientLibrary lib;
				PrintOperateResult(lib.BasicOperDeleteFile(argv[2]));
			}
		}
		else if(command == "status")
		{
			JClientLibrary lib;
			PrintOperateResult(lib.BasicOperCheckFile(argv[2]));
		}
		break;
	case 4:
		//jsvn move foo abc
		if(command == "move")
		{
			if(argc != 4)
			{
				printf("Type \"jsvn help move\" for help.");
			}
			else
			{
				JClientLibrary lib;
				PrintOperateResult(lib.BasicOperMoveFile(argv[2],argv[3]));
			}
		}
		break;
	case 5:
		break;
	case 6:
		if(command == "create")
		{
			if(argc != 6)
			{
				printf("Type \"jsvn help create\" for help.");
			}
			else
			{
				JClientLibrary lib;
				std::string errorMsg = lib.CreateWorkingCopies(argv[2],argv[3],argv[4],argv[5]);
				printf("%s\n",errorMsg.c_str());
			}
		}
		break;
	}
}

//bool JCommandLine::IsWorkingPath(std::string& rootPath,std::string& currentPath)
//{
//	std::string rootPath = JWorkingCopies::FindRootDir(currentPath);
//	if(rootPath.empty())
//	{
//		printf("jsvn: warning: \"%s\" is not working path.",currentPath);
//		return false;
//	}
//	return true;
//}

std::string JCommandLine::IsInputLegal( int argc,char* argv[] )
{
	return "";
}

void JCommandLine::PrintOperateResult( JOperateFileResult result )
{
	if(!result.errorMsg.empty())
	{
		printf("Error message:%s\n",result.errorMsg.c_str());
	}
	std::list<JWorkingCopiesBaseFile>::iterator ite2 = result.originalNotInWorkingCopies.begin();
	for(;ite2 != result.originalNotInWorkingCopies.end(); ++ite2)
	{
		printf("%-4s %-4d %-4d %-20s %-20s\n",JWorkingCopiesBaseFile::FileOperationToString(ite2->operate).c_str(),ite2->lastChangeVersion,ite2->version,ite2->author.c_str(),ite2->fullFileName.c_str());
	}
	std::list<JWorkingCopiesBaseFile*>::iterator ite = result.workingCopies.begin();
	for(;ite != result.workingCopies.end(); ++ite)
	{
		printf("%-4s %-4d %-4d %-20s %-20s\n",JWorkingCopiesBaseFile::FileOperationToString((*ite)->operate).c_str(),(*ite)->lastChangeVersion,(*ite)->version,(*ite)->author.c_str(),(*ite)->fullFileName.c_str());
	}

}

//void JCommandLine::BasicFileOperation( int argc,char* argv[] )
//{
//
//}
